package cn.itcast.filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * TODO
 *
 * @Author mr.wu
 * Date 2025/1/318:31
 */

public class AuthorizeFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取请求参数
        MultiValueMap<String, String> params = exchange.getRequest().getQueryParams();

        // 2.获取请求头
        String authorization = params.getFirst("authorization");

        if("admin".equals(authorization)){
            return chain.filter(exchange);

        }
        exchange.getResponse().setStatusCode( HttpStatus.FORBIDDEN);
        return  exchange.getResponse().setComplete();


        // 3.获取
    }
}
